﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using DataBase;

namespace DBService
{
	public class DbSlab : DbObject
	{
		[Category("属性")]
		[Description("建筑模型Id")]
		[DisplayName("建筑模型Id")]
		[ReadOnly(true)]
		public int RacId
		{
			get
			{
				return this.mRacId;
			}
			set
			{
				this.mRacId = value;
			}
		}

		[Category("属性")]
		[Description("结构模型Id")]
		[DisplayName("结构模型Id")]
		[ReadOnly(true)]
		public int RstId
		{
			get
			{
				return this.mRstId;
			}
			set
			{
				this.mRstId = value;
			}
		}

		[Category("属性")]
		[Description("厚度")]
		[DisplayName("厚度(mm)")]
		[ReadOnly(true)]
		public double Thick
		{
			get
			{
				return this.mThick;
			}
			set
			{
				this.mThick = value;
			}
		}

		[Category("属性")]
		[Description("边路径")]
		[DisplayName("边路径")]
		[ReadOnly(true)]
		public DbEdgeLoop EdgeLoop
		{
			get
			{
				return this.mEdgeLoop;
			}
		}

		[Category("属性")]
		[Description("洞口集合")]
		[DisplayName("洞口集合")]
		[ReadOnly(true)]
		public List<DbOpening> OpeningList
		{
			get
			{
				return this.mOpeningList;
			}
		}

		[Category("属性")]
		[Description("建筑模型路径")]
		[DisplayName("建筑模型路径")]
		[ReadOnly(true)]
		public string ArchPath
		{
			get
			{
				return DbProject.GetFromDB(this.mProjectId, this.mDatabase).RacFilePath;
			}
		}

		[Category("属性")]
		[Description("结构模型路径")]
		[DisplayName("结构模型路径")]
		[ReadOnly(true)]
		public string RstPath
		{
			get
			{
				return DbProject.GetFromDB(this.mProjectId, this.mDatabase).RstFilePath;
			}
		}

		public DbSlab(int projectId, int racId, int rstId, double thick, DbEdgeLoop edgeLoop, List<DbOpening> openingList)
		{
			this.mProjectId = projectId;
			this.mEdgeLoop = edgeLoop;
			this.mRacId = racId;
			this.mRstId = rstId;
			this.mThick = thick;
			this.mOpeningList = openingList;
		}

		public static DbSlab GetFromDB(int id, IDatabase db)
		{
			DbSlab result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("SELECT ProjectId,Thick,RacId,RstId FROM TBSLAB WHERE Id={0}", id);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable == null)
				{
					result = null;
				}
				else
				{
					double thick = Convert.ToDouble(dataTable.Rows[0]["Thick"]);
					int racId = Convert.ToInt32(dataTable.Rows[0]["RacId"]);
					int rstId = Convert.ToInt32(dataTable.Rows[0]["RstId"]);
					result = new DbSlab(Convert.ToInt32(dataTable.Rows[0]["ProjectId"]), racId, rstId, thick, DbSlab.GetEdgeLoopOfSlab(id, db), DbSlab.GetOpeningsOfSlab(id, db))
					{
						mId = id,
						mDatabase = db
					};
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static List<DbSlab> GetFromDB(IDatabase db)
		{
			List<DbSlab> result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				List<DbSlab> list = new List<DbSlab>();
				string sql = string.Format("SELECT Id,ProjectId,Thick,RacId,RstId FROM TBSLAB", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable == null)
				{
					result = list;
				}
				else
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						int num = Convert.ToInt32(dataTable.Rows[i]["Id"]);
						double thick = Convert.ToDouble(dataTable.Rows[i]["Thick"]);
						int racId = Convert.ToInt32(dataTable.Rows[i]["RacId"]);
						int rstId = Convert.ToInt32(dataTable.Rows[i]["RstId"]);
						list.Add(new DbSlab(Convert.ToInt32(dataTable.Rows[i]["ProjectId"]), racId, rstId, thick, DbSlab.GetEdgeLoopOfSlab(num, db), DbSlab.GetOpeningsOfSlab(num, db))
						{
							mId = num,
							mDatabase = db
						});
					}
					result = list;
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static void Clear(DbProject project)
		{
			foreach (DbSlab dbSlab in DbSlab.GetByProject(project))
			{
				dbSlab.RemoveFromDb();
			}
		}

		public static List<DbSlab> GetByProject(DbProject project)
		{
			List<DbSlab> result;
			try
			{
				if (project.Database.Connection.State == ConnectionState.Closed)
				{
					project.Database.Connection.Open();
				}
				List<DbSlab> list = new List<DbSlab>();
				string sql = string.Format("SELECT Id,ProjectId,Thick,RacId,RstId FROM TBSLAB WHERE ProjectId={0}", project.Id);
				DataTable dataTable = project.Database.ExecQuery(sql);
				if (project.Database.Connection.State == ConnectionState.Open)
				{
					project.Database.Connection.Close();
				}
				if (dataTable == null || dataTable.Rows.Count <= 0)
				{
					result = list;
				}
				else
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						int num = Convert.ToInt32(dataTable.Rows[i]["Id"]);
						double thick = Convert.ToDouble(dataTable.Rows[i]["Thick"]);
						int racId = Convert.ToInt32(dataTable.Rows[i]["RacId"]);
						int rstId = Convert.ToInt32(dataTable.Rows[i]["RstId"]);
						list.Add(new DbSlab(Convert.ToInt32(dataTable.Rows[i]["ProjectId"]), racId, rstId, thick, DbSlab.GetEdgeLoopOfSlab(num, project.Database), DbSlab.GetOpeningsOfSlab(num, project.Database))
						{
							mId = num,
							mDatabase = project.Database
						});
					}
					result = list;
				}
			}
			catch (Exception ex)
			{
				if (project.Database.Connection.State == ConnectionState.Open)
				{
					project.Database.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		private static DbEdgeLoop GetEdgeLoopOfSlab(int slabId, IDatabase db)
		{
			DbEdgeLoop result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				new List<DbEdge>();
				string sql = string.Format("SELECT EdgeLoopId FROM tbedgeloopofslab WHERE SlabId={0}", slabId);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable == null || dataTable.Rows.Count <= 0)
				{
					result = null;
				}
				else
				{
					result = DbEdgeLoop.GetFromDB(Convert.ToInt32(dataTable.Rows[0]["EdgeLoopId"]), db);
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		private static List<DbOpening> GetOpeningsOfSlab(int slabId, IDatabase db)
		{
			List<DbOpening> result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				List<DbOpening> list = new List<DbOpening>();
				string sql = string.Format("SELECT OpeningId FROM tbopeningofslab WHERE SlabId={0}", slabId);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					DbOpening fromDB = DbOpening.GetFromDB(Convert.ToInt32(dataTable.Rows[i]["OpeningId"]), db);
					list.Add(fromDB);
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public override void AddToDb(IDatabase db)
		{
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("INSERT INTO tbslab (projectId, RstId,RacId,Thick) VALUES ({0},{1},{2},{3})", new object[]
				{
					this.mProjectId,
					this.mRstId,
					this.mRacId,
					this.mThick
				});
				db.ExecuteCommand(sql);
				this.mDatabase = db;
				string sql2 = string.Format("select max(Id) from tbslab", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql2);
				this.mId = Convert.ToInt32(dataTable.Rows[0][0]);
				this.mDatabase = db;
				foreach (DbOpening dbOpening in this.mOpeningList)
				{
					dbOpening.AddToDb(db);
					if (db.Connection.State == ConnectionState.Closed)
					{
						db.Connection.Open();
					}
					string sql3 = string.Format("INSERT INTO tbopeningofslab (SlabId, OpeningId) VALUES ({0},{1})", base.Id, dbOpening.Id);
					db.ExecuteCommand(sql3);
				}
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
		}

		public override void UpdateToDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("UPDATE tbslab SET ProjectId={0},RstId = {1},RacId={2},Thick={3} WHERE Id ={4}", new object[]
				{
					this.mProjectId,
					this.mRstId,
					this.mRacId,
					this.mThick,
					base.Id
				});
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public override void RemoveFromDb()
		{
			if (base.Database.Connection.State == ConnectionState.Closed)
			{
				base.Database.Connection.Open();
			}
			using (DbTransaction dbTransaction = base.Database.StartTransaction())
			{
				try
				{
					string sql = string.Format("DELETE FROM tbslab WHERE Id = {0}", base.Id);
					base.Database.ExecuteCommand(sql, dbTransaction);
					this.mEdgeLoop.RemoveFromDb();
					if (base.Database.Connection.State == ConnectionState.Closed)
					{
						base.Database.Connection.Open();
					}
					string sql2 = string.Format("DELETE FROM tbedgeloopofslab WHERE SlabId={0}", base.Id);
					base.Database.ExecuteCommand(sql2, dbTransaction);
					foreach (DbOpening dbOpening in this.OpeningList)
					{
						dbOpening.RemoveFromDb();
						if (base.Database.Connection.State == ConnectionState.Closed)
						{
							base.Database.Connection.Open();
						}
						string sql3 = string.Format("DELETE FROM tbopeningofslab WHERE SlabId={0}", base.Id);
						base.Database.ExecuteCommand(sql3, dbTransaction);
					}
					if (base.Database.Connection.State == ConnectionState.Closed)
					{
						base.Database.Connection.Open();
					}
					dbTransaction.Commit();
					if (base.Database.Connection.State == ConnectionState.Open)
					{
						base.Database.Connection.Close();
					}
				}
				catch (Exception ex)
				{
					if (base.Database.Connection.State == ConnectionState.Closed)
					{
						base.Database.Connection.Open();
					}
					dbTransaction.Rollback();
					if (base.Database.Connection.State == ConnectionState.Open)
					{
						base.Database.Connection.Close();
					}
					throw ex;
				}
			}
		}

		public override string ToString()
		{
			return string.Format("结构({0})-建筑({1})", this.mRstId.ToString(), this.mRacId.ToString());
		}

		private int mProjectId;

		private int mRacId;

		private int mRstId;

		private double mThick;

		public DbEdgeLoop mEdgeLoop = new DbEdgeLoop();

		public List<DbOpening> mOpeningList = new List<DbOpening>();
	}
}
